package com.dingwen.treasure.business.controller;

import cn.hutool.core.util.IdUtil;
import com.dingwen.treasure.base.controller.BaseViewController;
import com.dingwen.treasure.base.pojo.vo.Result;
import com.dingwen.treasure.business.component.event.SubscribeDisinfectServerEvent;
import com.dingwen.treasure.mybatisplus.service.TreasureMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 　MessageController: 消息可靠投递、可靠、顺序消费案例
 * 　@author dingwen
 * 　@date 2022/8/9
 */
@Api(tags = "可靠投递有序消费可靠消费")
@RestController
@Slf4j
@RequestMapping("message")
public class MessageController implements BaseViewController {

    /**
     * 应用程序上下文
     */
    @Resource
    private  ApplicationContext applicationContext;


    /**
     * 消息服务
     */
    @Resource
    private  TreasureMessageService messageService;

    @GetMapping
    @ApiOperation(value = "模拟订阅消杀业务", response = Result.class)
    public void test() {
        // step1: 用户订阅消杀服务
        // step2: 用户支付
        // step3: 通知消杀公司
        // step4; 通知用户
        log.info("消杀服务订阅，发布事件...");
        applicationContext.publishEvent(new SubscribeDisinfectServerEvent(IdUtil.getSnowflake().nextId()));

    }

    @PutMapping("deliver")
    @ApiOperation(value = "消息投递")
    public Result<String> feignTreasureMessageDeliver() {
        messageService.deliverMessage();
        return success();
    }
}
